package com.wtx.chapter03;

import java.util.Random;

/**
 * @description: 队列的效率测试
 * @author: wtx
 * @createDate: 2020/5/17
 */
public class TestQueue {

    public static void main(String[] args) {

        int opCount = 100000;
        ArrayQueue<Integer> arrayQueue = new ArrayQueue<>();
        double time1 = test(arrayQueue, opCount);
        System.out.println("ArrayQueue "+time1+"秒");

        System.out.println("================================");

        LoopQueue<Integer> loopQueue = new LoopQueue<>();
        double time2 = test(loopQueue, opCount);
        System.out.println("LoopQueue "+time2+"秒");
    }

    private static double test(Queue<Integer> q, int opCount){

        //start
        long startTime = System.nanoTime();

        Random random = new Random();
        for (int i = 0; i < opCount; i++) {
            q.enqueue(random.nextInt(Integer.MAX_VALUE));
        }
        for (int i = 0; i < opCount; i++) {
            q.dequeue();
        }

        //end
        long endTime = System.nanoTime();

        return (endTime - startTime) / 1000000000.0;
    }
}
